package DAO;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import model.ConnectionProperty;

public class DatabasePool {
	
	private static Map<String, Connection> connectionMap = 
		new HashMap<String, Connection>();
	private static Map<String, ConnectionProperty> connectionPropertyMap = 
		new HashMap<String, ConnectionProperty>();

	private static void createConnection(final ConnectionProperty properties) 
	throws SQLException, ClassNotFoundException {
		try {
			Class.forName(properties.getDriver());
			final Connection connection = 
				DriverManager.getConnection(properties.getUrl(), 
						properties.getUser(), properties.getPassword());
			connectionMap.put(properties.getId(), connection);
			connectionPropertyMap.put(properties.getId(), properties);
		} catch (final SQLException e) {
			throw new SQLException("Error trying to get connection to" +
					" Id: "+properties.getId()+
					" Driver: "+properties.getDriver()+
					" Url: "+properties.getUrl()+
					". Error: "+e.getMessage(), e.getSQLState());
		}
	}

	private static boolean isCreated(final ConnectionProperty properties) {
		if (connectionMap.containsKey(properties.getId())) {
			return true;
		}
		return false;
	}

		
	public static void create(final ConnectionProperty properties) 
	throws SQLException, ClassNotFoundException {
		if (!isCreated(properties)) {
			createConnection(properties);
		} else {
			throw new SQLException("You are trying to create a database connection " +
					"which already exists to" +
					" Id: "+properties.getId()+
					" Driver: "+properties.getDriver()+
					" Url: "+properties.getUrl()+
					". Please, check if the jod id is unique", "");
		}
	}

	public static Connection getConnection(final String idConnection) throws SQLException {
		if (connectionMap.containsKey(idConnection)) 
			return connectionMap.get(idConnection);
		return null;
	}
	
	public static String getSchema(final String idConnection) throws SQLException {
		String schema = "";
		if (connectionPropertyMap.containsKey(idConnection)) {
			schema = connectionPropertyMap.get(idConnection).getSchema();
			if (!"".equalsIgnoreCase(schema))
				schema = schema+".";
		}	
		return schema;
	}

	public static void close(final String idConnection) 
	throws SQLException, ClassNotFoundException {
		if (connectionMap.containsKey(idConnection)) {
			connectionMap.get(idConnection).close();
			connectionMap.remove(idConnection);
		}	
		if (connectionPropertyMap.containsKey(idConnection))
			connectionPropertyMap.remove(idConnection);
	}

	public static void commit(final String idConnection) 
	throws SQLException, ClassNotFoundException {
		if (connectionMap.containsKey(idConnection)) 
			connectionMap.get(idConnection).commit();
	}
	
	public static void rollback(final String idConnection) 
	throws SQLException, ClassNotFoundException {
		if (connectionMap.containsKey(idConnection)) 
			connectionMap.get(idConnection).rollback();
	}

	public static void setAutoCommit(final String idConnection, final boolean autoCommit) 
	throws SQLException, ClassNotFoundException {
		if (connectionMap.containsKey(idConnection)) 
			connectionMap.get(idConnection).setAutoCommit(autoCommit);
	}

}
